package com.example.springboot01.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.springboot01.entity.Professor;
import com.example.springboot01.entity.ProfessorCourse;
import com.example.springboot01.resp.Response;
import com.example.springboot01.resp.enums.CommonRespEnum;
import com.example.springboot01.resp.enums.ProfessorRespEnum;
import com.example.springboot01.service.ProfessorCourseService;
import com.example.springboot01.service.ProfessorService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/v1/professorCourse")
public class ProfessorCourseController {

    @Resource
    private ProfessorCourseService professorService;

    @PostMapping
    public Response add(@RequestBody ProfessorCourse professor){


        final List<Integer> courseIds = professor.getCourseIds();
        if (courseIds == null || courseIds.isEmpty()) {
            return Response.ok(null);
        }

        professorService.remove(new LambdaQueryWrapper<ProfessorCourse>()
                .eq(ProfessorCourse::getProfessorId, professor.getProfessorId())
        );

        List<ProfessorCourse> professorCourses = new ArrayList<>();
        for (Integer c : courseIds) {
            ProfessorCourse course = new ProfessorCourse();
            course.setCourseId(c);
            course.setProfessorId(professor.getProfessorId());
            professorCourses.add(course);
        }
        professorService.saveBatch(professorCourses);
        return Response.ok(null);
    }


    @GetMapping
    public Response list(){
        return Response.ok(professorService.list());
    }


    @GetMapping("/{professorId}")
    public Response list(@PathVariable Integer professorId){
        return Response.ok(professorService.list(new LambdaQueryWrapper<ProfessorCourse>()
                .eq(ProfessorCourse::getProfessorId, professorId)
        ));
    }
}
